package com.xy.biz.base.service.impl;

import com.xy.biz.base.domain.req.GetUnitConvertRequest;
import com.xy.biz.base.domain.resp.GetUnitConvertResponse;
import com.xy.biz.base.manager.UnitConvertManager;
import com.xy.biz.base.service.UnitConvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class UnitConvertServiceImpl implements UnitConvertService {

    @Autowired
    private UnitConvertManager unitConvertManager;

    @Override
    public GetUnitConvertResponse getMultiplier(GetUnitConvertRequest request) {
        String converteeUnit = request.getConverteeUnit().toLowerCase();
        String converterUnit = request.getConverterUnit().toLowerCase();
        if (Objects.equals(converteeUnit, converterUnit)) {
            return GetUnitConvertResponse.builder().multiplier(1).asc(true).build();
        }
        if (Objects.isNull(request.getConverteeUnit())
                || Objects.isNull(request.getConverterUnit())
                || !StringUtils.hasText(request.getConverteeUnit().trim())
                || !StringUtils.hasText(request.getConverterUnit().trim())) {
            return GetUnitConvertResponse.builder().multiplier(1).asc(true).build();
        }
        Map<String, Map<String, Integer>> map = unitConvertManager.mapUnitConvert();
        return getMultiplier(converteeUnit, converterUnit, map);
    }

    GetUnitConvertResponse getMultiplier(String converteeUnit, String converterUnit,
                                         Map<String, Map<String, Integer>> topMap) {
        Integer multiplier = null;
        boolean asc = true;
        Map<String, Integer> map = topMap.get(converteeUnit);
        if (map == null) {
            map = topMap.get(converterUnit);
            if (map != null) {
                multiplier = map.get(converteeUnit);
                if (multiplier != null) {
                    asc = false;
                }
            }
        } else {
            multiplier = map.get(converterUnit);
            if (multiplier != null) {
                asc = true;
            } else {
                map = topMap.get(converterUnit);
                if (map != null) {
                    multiplier = map.get(converteeUnit);
                    if (multiplier != null) {
                        asc = false;
                    }
                }
            }
        }


        return GetUnitConvertResponse.builder().multiplier(multiplier).asc(asc).build();
    }


    public static void main(String[] args) {
        Map<String, Map<String, Integer>> topMap = new HashMap<>();

        Map<String, Integer> map = new HashMap<>();
        map.put("l", 1000);
        topMap.put("ml", map);
        map = new HashMap<>();
        map.put("l", 1000000);
        topMap.put("ul", map);
        map = new HashMap<>();
        map.put("kl", 1000);
        topMap.put("l", map);

        UnitConvertServiceImpl impl = new UnitConvertServiceImpl();
        GetUnitConvertResponse response = impl.getMultiplier("ml", "l", topMap);
        System.out.println(response.getMultiplier() + "-" + response.isAsc());
        response = impl.getMultiplier("kl", "l", topMap);
        System.out.println(response.getMultiplier() + "-" + response.isAsc());
        response = impl.getMultiplier("l", "kl", topMap);
        System.out.println(response.getMultiplier() + "-" + response.isAsc());
        response = impl.getMultiplier("ml", "kl", topMap);
        System.out.println(response.getMultiplier() + "-" + response.isAsc());
        response = impl.getMultiplier("ul", "ll", topMap);
        System.out.println(response.getMultiplier() + "-" + response.isAsc());
    }

}
